//
//  AVOSUserManger.m
//  FunnyVideo
//
//  Created by 李鑫 on 15/10/19.
//  Copyright © 2015年 NewEggTech. All rights reserved.
//

#import "AVOSUserManger.h"
#import <AVOSCloud/AVOSCloud.h>

@implementation AVOSUserManger
@synthesize user;
- (id)init{
    self = [super init];
    
    if(self)
    {
         user = [AVUser currentUser];
    }
    
    return self;
}

- (AVUser *)getLoginUser{
    return self.user;
}
- (void)setLoginUser:(AVUser *)user_{
    user = user_;
}

- (NSString *)getSession{
    return [self.user sessionToken];
}
- (NSString *)getUserName{
    return [self.user objectForKey:@"username"];
}
- (UIImage *)getUserFace{
    AVFile *faceFile = [self.user objectForKey:@"userFace"];
    NSData *data = [faceFile getData];
    return [UIImage imageWithData:data];
}

+ (void)loginWithUsername:(NSString *)user Psw:(NSString *)psw completion:(void (^)(AVUser *user ,BOOL succ,NSError *error))loginCompletion{
    [AVUser logInWithUsernameInBackground:user password:psw block:^(AVUser *user, NSError *error) {
        if(!error)
        {
            loginCompletion(user,YES,error);
        }
        else
            loginCompletion(nil,NO,error);
    }];
}


+ (void)loginWithMobile:(NSString *)phone Psw:(NSString *)psw completion:(void(^)(AVUser *user ,BOOL succ,NSError *error))loginCompletion{
    [AVUser logInWithMobilePhoneNumberInBackground:phone password:psw block:^(AVUser *user, NSError *error) {

        if(!error)
        {
            loginCompletion(user,YES,error);
        }
        else
            loginCompletion(nil,NO,error);
    }];
}

#pragma mark -- 注册事件
+ (void)registWithMobile:(NSString *)phone Psw:(NSString *)psw smsCode:(NSString *)smsCode completion:(void(^)(AVUser *user ,BOOL succ,NSError *error))loginCompletion{
    [AVUser signUpOrLoginWithMobilePhoneNumberInBackground:phone smsCode:smsCode block:^(AVUser *user, NSError *error) {
        if(!error)
        {
            loginCompletion(user,YES,error);
        }
        else
            loginCompletion(nil,NO,error);
    }];
}

+ (void)getUserFaceByUserName:(NSString *)userName completion:(void(^)(UIImage *head,NSError *error))completion{
    AVQuery *query = [AVQuery queryWithClassName:@"_User"];
    
    [query whereKey:@"username" equalTo:userName];
    
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        AVUser *user = [objects lastObject];
        
        AVFile* file = [user objectForKey:@"userFace"];
        
        NSError *error_ = nil;
        
        NSData *headData = [file getData:&error_];

        UIImage *headImg = [UIImage imageWithData:headData];
        
        completion(headImg,error);
    }];
}

+ (AVOSUserManger *)sharedManger{
    static dispatch_once_t onceToken;
    static AVOSUserManger  *um;
    dispatch_once(&onceToken, ^{
        um = [[AVOSUserManger alloc] init];
    });
    return um;
}

+ (void)updateUser{
    [[AVOSUserManger sharedManger] update];
}

- (void)update{
    user = [AVUser currentUser];
}

#pragma mark -- User Info check
- (BOOL)hasUserInfo{
    return [[AVUser currentUser] isAuthenticated];
}

- (NSString *)userSession{
    return [[AVUser currentUser] sessionToken];
}

@end
